﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SuperSocketClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect("127.0.0.1", 9514);
            NetworkStream netStream = tcpClient.GetStream();
            while (true)
            {
                try
                {

                    string tcpdata = "天下无敌!";
                    byte[] byteArray = Encoding.UTF8.GetBytes(tcpdata);
                    byte[] byteArrayLength = Encoding.UTF8.GetBytes("\r\n");//\r\n

                    byte[] combined = new byte[byteArray.Length + 2];
                    byteArray.CopyTo(combined, 0);
                    byteArrayLength.CopyTo(combined, byteArray.Length);

                    netStream.Write(combined, 0, combined.Length);
                    //rb.IsChecked = false;
                }
                catch (Exception ex)
                {
                    netStream.Close();
                    tcpClient.Close();
                    MessageBox.Show(ex.ToString());
                }
                await Task.Delay(5000);
            }
        }
    }
}
